package com.v512;

//import java.io.File;

//import java.io.FileInputStream;
//import java.io.IOException;
//
//import javax.servlet.ServletConfig;
//import javax.servlet.ServletContext;
//import javax.servlet.ServletException;
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//public class DownloadOtherFileServlet extends HttpServlet {
//	private static final long serialVersionUID = 1L;
//	
//	private ServletContext sc;
//	private String savePath;
//
//	public void init(ServletConfig config) throws ServletException {
//		savePath = config.getInitParameter("savePath");
//		sc = config.getServletContext();
//	}
//	
//	protected void doPost(HttpServletRequest request,
//			HttpServletResponse response) throws ServletException, IOException {
//
//		String fileAddress = request.getParameter("fileaddress");
//		System.out.println();
//		if(fileAddress == null || fileAddress.equals("")) {
//			fileAddress = "无法下载该文件";
//			System.out.println("未找到文件路径");
//		}
//		String path = request.getSession().getServletContext().getRealPath(fileAddress);
//		File file = new File(fileAddress);
//		response.setContentType("application/octet-stream");
//		response.setContentLength((int) file.length());
//		response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
//
//		
//		FileInputStream in = new FileInputStream(fileAddress);
//		ServletOutputStream out = response.getOutputStream();
//		byte[] buffer = new byte[1024];
//		int len = -1;
//		while ((len = in.read(buffer)) != -1) {
//			out.write(buffer, 0, len);
//		}
//		in.close();
//		out.close();
//	}
//}

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class DownloadOtherFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ServletContext sc;
	private String savePath;

	public void init(ServletConfig config) throws ServletException {
		savePath = config.getInitParameter("savePath");
		sc = config.getServletContext();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> it = items.iterator();
			while (it.hasNext()) {
				FileItem item = (FileItem) it.next();
				if (item.isFormField()) {
					request.setAttribute("fieldName", item.getFieldName());
					request.setAttribute("parameterName", item.getString("UTF-8"));
				} else {
					if (item.getName() != null && !item.getName().equals("")) {
						File file = new File(sc.getRealPath("/") + savePath, item.getName());
						item.write(file);
						System.out.println("文件已上传至库中");

						String fileName = file.getName();
						response.addHeader("Content-Type", "application/octet-stream");
						response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
						String path = request.getSession().getServletContext().getRealPath("/uploads/");
						FileInputStream in = new FileInputStream(path + fileName);

						ServletOutputStream out = response.getOutputStream();
						byte[] buffer = new byte[4096];
						int len = -1;
						while ((len = in.read(buffer)) != -1) {
							out.write(buffer, 0, len);
						}
						in.close();
						out.close();
						System.out.println("文件下载成功");

					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}